﻿using B.S.BasicData.Api.Write.Application.Command.Approval;
using B.S.BasicData.Domain.Entity.Approval;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infastructure.Interface;
using MediatR;

namespace B.S.BasicData.Api.Write.Application.Handler.Approval
{
    public class DeleteApprovalHandler : IRequestHandler<DeleteApprovalCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<ApprovalModel> _approvalRepository;

        public DeleteApprovalHandler(IBaseRepository<ApprovalModel> approvalRepository)
        {
            _approvalRepository = approvalRepository;
        }

        /// <summary>
        /// 申请删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiResult<int>> Handle(DeleteApprovalCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();

            var approval = _approvalRepository.GetAll().Where(p =>p.Id==request.Id && p.IsDelete == false).FirstOrDefault();
            if (approval!=null)
            {
                approval.IsDelete=true;
            }
            var res = _approvalRepository.Update(approval);
            if (res > 0)
            {
                result.Data = res;
                result.Code = StatusEnum.OK;
                result.Message = "删除成功";
            }
            else
            {
                result.Data = res;
                result.Code = StatusEnum.Fail;
                result.Message = "删除失败";
            }
            return Task.FromResult(result);
        }
    }
}
